package com.xmcowell.sportpacemaster.base;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import com.xmcowell.sportpacemaster.config.Contents;
import com.xmcowell.sportpacemaster.layout.MainActivity;
import com.xmcowell.sportpacemaster.layout.app.AppActivity;
import com.xmcowell.sportpacemaster.layout.i_run.IRunActivity;
import com.xmcowell.sportpacemaster.layout.me.PersonalActivity;
import com.xmcowell.sportpacemaster.layout.multi.MultiActivity;
import com.xmcowell.sportpacemaster.layout.setting.SettingActivity;
import com.xmcowell.sportpacemaster.layout.setting.TreadmillInfo.TreadmillInfoActivity;

/**
 * Created by Administrator on 2017/7/20 0020.
 */

public class USBService extends Service {
    private static final String TAG = "BroadCastTest";

    BroadcastReceiver mReceiver;

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.e(TAG, "USBService oncreate");
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.e(TAG, "USBService onstart");
        mReceiver = new BroadcastReceiver() {
            public void onReceive(Context context, Intent intent) {
                Log.e(TAG, "USBService action = "+intent.getAction());
                if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED)) {
                    Log.e(TAG, "Received SDCard Mount Event!");
                    showUSBConntectStatus(true);
                } else if (intent.getAction().equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
                    Log.e(TAG, "Received SDCard UnMount Event!");
                    showUSBConntectStatus(false);
                } else if (intent.getAction().equals("android.hardware.usb.action.USB_STATE")) {
                    boolean connected = intent.getExtras().getBoolean("android.hardware.usb.action.USB_STATE");
                    Log.e(TAG, "Received Usb event, Usb connect status is:" + connected);
                    if (connected) {
                        showUSBConntectStatus(true);
                    } else {
                        showUSBConntectStatus(false);
                    }
                }
            }
        };
        // Fileter for SDCard mounted and unmounted
        IntentFilter filterSDCard = new IntentFilter();
        filterSDCard.addAction(Intent.ACTION_MEDIA_MOUNTED);
        filterSDCard.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        // must add so the receiver can receive the SD card mount and unmount
        // event
        filterSDCard.addDataScheme("file");
        registerReceiver(mReceiver, filterSDCard);

        //Filter for usb connect
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.hardware.usb.action.USB_STATE");
        registerReceiver(mReceiver, filter);
    }

    private void showUSBConntectStatus(boolean b) {
        Message msg = new Message();
        msg.what = Contents.USB;
        Bundle bundle = new Bundle();
        bundle.putBoolean("flag", b);
        msg.setData(bundle);
        if (MainActivity.instance != null && !MainActivity.instance.isFinishing()) {
            MainActivity.instance.mHandler.sendMessage(msg);
        }
        if (IRunActivity.instance != null && !IRunActivity.instance.isFinishing()) {
            IRunActivity.instance.mHandler.sendMessage(msg);
        }
        if (MultiActivity.instance != null && !MultiActivity.instance.isFinishing()) {
            try {
                MultiActivity.instance.mHandler.sendMessage(msg);
            }catch (Exception e){
                //  报一个错误 暂时捕获吧
            }

        }
        if (AppActivity.instance != null && !AppActivity.instance.isFinishing()) {
            AppActivity.instance.mHandler.sendMessage(msg);
        }
        if (PersonalActivity.instance != null && !PersonalActivity.instance.isFinishing()) {
            PersonalActivity.instance.mHandler.sendMessage(msg);
        }
        if (SettingActivity.instance != null && !SettingActivity.instance.isFinishing()) {
            SettingActivity.instance.handler.sendMessage(msg);
        }
        if (TreadmillInfoActivity.instance!=null&&!TreadmillInfoActivity.instance.isFinishing()){
            TreadmillInfoActivity.instance.mHandler.sendMessage(msg);
        }
    }

}
